﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;
using WebApp.AppCode;

public partial class Manage_RoleManage_RoleList : System.Web.UI.Page
{
    public CompetenceLib CL = new CompetenceLib();
    Dcms_RoleBLL rBll = new Dcms_RoleBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            CL.IsLogin();

            if (!IsPostBack)
            {
                string strDesignRoleID = ConfigurationManager.AppSettings["DesignRoleID"];
                this.hidAmount.Value = rBll.GetDataCount(("select count(*) from Dcms_Role where Role_ID <> " + strDesignRoleID).ToString());
                this.hidSql.Value = "select * from Dcms_Role where Role_ID <> " + strDesignRoleID;
                ReBinder(1);
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_RoleManage_RoleList.Page_Load", ex);
            throw ex;
        }
    }

    protected void BindRpt(object sender, PagingEventArgs e)
    {
        this.ReBinder(e.pageIndex);
    }

    private void ReBinder(int PageIndex)
    {
        this.repRole.DataSource = rBll.GetDataListByPage(this.hidSql.Value, PageIndex, 11);
        this.repRole.DataBind();
        pagingctrl.InitPc(Convert.ToInt32(this.hidAmount.Value), PageIndex, 10);
    }

    protected void repRole_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.Item.ItemIndex % 2 == 1)
            {
                ((HtmlTableRow)e.Item.FindControl("tr1")).Style.Add("background", "#F2F2F2");
            }

            ((HyperLink)e.Item.FindControl("hypEdit")).NavigateUrl = String.Format
                ("AddRole.aspx?RolID=" + ((HiddenField)e.Item.FindControl("hidID")).Value + "&CID=" + strCateID);
            
            ((LinkButton)e.Item.FindControl("HypDelete")).Attributes.Add
                    ("onclick", "return confirm('确定要删除该记录吗？');");

            ((HyperLink)e.Item.FindControl("hypPopedom")).NavigateUrl = String.Format
                ("PopedomManage.aspx?RolID=" + ((HiddenField)e.Item.FindControl("hidID")).Value + "&CID=" + strCateID);
        }
    }

    protected void repRole_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (e.CommandName == "delete")
            {
                rBll.DeleteModelByID(Convert.ToInt32(((HiddenField)e.Item.FindControl("hidID")).Value));

                Session.Add("Message", "删除角色成功，请返回！");
                Session.Add("Url", "RoleManage/RoleList.aspx?id=" + strCateID);
                Response.Redirect("../Message.aspx",false);
            }
        }
    }

    protected string strCateID
    {
        get
        {
            return Request.QueryString["id"] + "";
        }
    }
}
